package study.saas.web.portal.sys.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import study.saas.common.util.CsvExportUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.order.entity.Purchase;
import study.saas.order.service.PurchaseFacade;
import study.saas.order.service.model.PurchaseConditionDTO;
import study.saas.order.service.model.PurchaseCreateDTO;
import study.saas.order.service.model.PurchaseFilterDTO;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HuBoyu
 */

@Api(tags = "订单")
@RestController
@RequestMapping(value = "sys/purchase")
@Slf4j


public class PurchaseController {
    @Reference(version = "1.0.0")
    private PurchaseFacade purchaseFacade;

    @ApiOperation(value = "获取")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ApiResultV2<Purchase> get(Integer purchaseId) {
        if (purchaseId == null ) {
            return ApiResultV2.error("数据不能为空！");
        }

        return purchaseFacade.get(purchaseId);
    }

    @ApiOperation(value = "查询列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResultV2 list(PurchaseFilterDTO filter) {
        if (filter == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return purchaseFacade.list(filter);
    }

    @ApiOperation(value = "创建")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ApiResultV2 purchaseCreate(PurchaseCreateDTO purchaseCreate) {
        if (purchaseCreate == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return purchaseFacade.purchaseCreate(purchaseCreate);
    }


    @ApiOperation(value = "删除/根据订单ID删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ApiResultV2 deletePurchase(Integer purchaseId) {
        if (purchaseId == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return purchaseFacade.deletePurchase(purchaseId);
    }
    @ApiOperation(value = "条件查询")
    @RequestMapping(value = "/conditionList", method = RequestMethod.POST)
    public ApiResultV2 conditionList(PurchaseConditionDTO filter) {
        if (filter == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return purchaseFacade.conditionList(filter);
    }

    @ApiOperation(value = "导出")
    @RequestMapping(value = "/export",method = RequestMethod.GET)
    public  void export(HttpServletResponse response) throws IOException {
        try {
            // 查询需要导出的数据
            List<Purchase> purchasesList = purchaseFacade.selectAll().getData();
            // 构造导出数据结构
            // 设置表头
            String titles = "订单Id,商品Id,商品名称,商品数量,商品单价";
            // 设置每列字段
            String keys = "purchaseId,id,purchaseName,amount,price";

            // 构造导出数据
            List<Map<String, Object>> datas = new ArrayList<>();
            Map<String, Object> mapInfo;

            for (Purchase data : purchasesList) {
                mapInfo = new HashMap<>(8);
                mapInfo.put("purchaseId", data.getPurchaseId());
                mapInfo.put("id", data.getId());
                mapInfo.put("purchaseName", data.getPurchaseName());
                mapInfo.put("amount", data.getAmount());
                mapInfo.put("price", data.getPrice());

                datas.add(mapInfo);
            }

            // 设置导出文件前缀
            String fName = "purchase_";

            // 文件导出
            OutputStream os = response.getOutputStream();
            CsvExportUtil.responseSetProperties(fName, response);
            CsvExportUtil.doExport(datas, titles, keys, os);
            os.close();
        } catch (Exception e) {
            log.error("导出失败" + e.getMessage(), e);
        }

    }

    }




